#!/usr/bin/python3
# -*- coding:utf-8 -*-
# __author__ == taoyulong2018@gmail.com
# __time__ == 2023/8/23 10:24
# ===========================================
#       题目名称： 28.找出字符串中第一个匹配项的下标
#       题目地址： https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string/
#       题目描述： https://note.youdao.com/s/RRXNSXjR
# ===========================================


class Solution:
    def strStr(self, haystack, needle):
        index = -1                  # 下标
        needle_prefix = needle[0]   # 前缀
        for i in range(len(haystack)):
            haystack_prefix = haystack[i]
            temp_haystack = haystack[i:i + len(needle)]
            if haystack_prefix == needle_prefix and needle == temp_haystack:
                index = i
                break
        return index


if __name__ == '__main__':
    s = Solution()
    # 0
    print(s.strStr(haystack="sadbutsad", needle="sad"))
    # -1
    print(s.strStr(haystack="leetcode", needle="leeto"))
    # 2
    print(s.strStr(haystack="hello", needle="ll"))
